let starttime = Date.now();

export const youdaoTranslate = async (text: string) => {
	let youdaoID = '1c9e91583cd7f940';
	let youdaoKey = 'll6iuVgNLpOOXkCy96z6RauplOfOS3g0';
	const axios = require('axios');
	const crypto = require('crypto');

	const salt = new Date().getTime();
	const curtime = Math.round(new Date().getTime() / 1000);
	let query = '  hello super owner this  is  your word ';
	query = text;
	// 多个query可以用\n连接  如 query='apple\norange\nbanana\npear'
	const from = 'auto';
	const to = 'zh-CHS';
	const vocabId = 'super';

	function truncate(q) {
		const len = q.length;
		if (len <= 20) return q;
		return q.substring(0, 10) + len + q.substring(len - 10, len);
	}

	const str1 = youdaoID + truncate(query) + salt + curtime + youdaoKey;
	const sign = crypto.createHash('sha256').update(str1).digest('hex');
	try {
		return await axios
			.post('https://openapi.youdao.com/api', null, {
				params: {
					q: query,
					appKey: youdaoID,
					salt: salt,
					from: from,
					to: to,
					sign: sign,
					signType: 'v3',
					curtime: curtime,
					vocabId: vocabId,
					domain: 'computers',
				},
			})
			.then((response) => {
				// console.log(response.data.translation[0], 'res 1 ');
				return Promise.resolve(response.data.translation[0]);
			})
			.catch((error) => {
				console.error(error);
				return Promise.reject(error);
			});
	} catch (e) {
		console.log(e);
		return Promise.reject(e);
	}
};

// let r = youdaoTranslate('hello super');
// console.log(r);

// console.log('start');

// console.log('end');
